<template>
	<view class="personal">
		<TopBar :indexflag="true" :combakflag="true" :lang="false" :bottom="true"
			:title="$t('ClassificationManagement')" :barstyle="colortopic()" Color="#fff"></TopBar>
		<view class="category-box" :dir="i18nLocale=='ug'?'rtl':''">
			<view class="cate-item" v-for="item in columns" @click="addcategory(item)">
				<view class="left">
					<u-icon name="arrow-up-fill" :color="colortopic()" size="20"></u-icon>
					<text>{{i18nLocale=='ug'?item.ug_name:item.cn_name}}</text>
				</view>
				<view class="right">
					<view class="button edit">
						<u-icon name="tags" :color="item.status?'#494FB6':'#cc0000'" size="22"></u-icon>
						<text
							:style="{color:item.status?'#494FB6':'#cc0000'}">{{item.status? $t('Shelves'):$t('TakenShelves')}}</text>
					</view>
					<view class="button edit">
						<u-icon name="edit-pen" color="#f9ae3d" size="22"></u-icon>
						<text>{{$t('Edit')}}</text>
					</view>
					<view class="button delet" @click.stop="deletcategory(item.id)">
						<u-icon name="trash" color="#cc0000" size="22"></u-icon>
						<text class="" style="#cc0000">{{ $t('Delete')}}</text>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="popshow" :round="10" mode="center" @close="close" @open="open">
			<view :class="i18nLocale=='ug'?' ug-from input-from':'input-from'">
				<view class="big-title">
					<text>{{$t('AddCategory')}}</text>
				</view>
				<view class="input-item">
					<view class="title">
						{{$t('MenuCategoryNameug')}}
					</view>
					<u--input :placeholder="$t('MenuCategoryNameug')" fontSize="28rpx" autoBlur border="bottom"
						:adjustPosition='false' v-model="form.ug_name" @blur="changeugblur"></u--input>
				</view>
				<view class="input-item">
					<view class="title">
						{{$t('MenuCategoryNamecn')}}
					</view>
					<u--input :placeholder="$t('MenuCategoryNamecn')" fontSize="28rpx" autoBlur border="bottom"
						:adjustPosition='false' v-model="form.cn_name" @blur="changecnblur"></u--input>
				</view>
				<view class="input-item" style="display: flex;justify-content: space-between;">
					<view class="title">
						{{$t('ShopType')}}
					</view>
					<view class="" :style="{color:form.status?'#494FB6':'#cc0000'}">
						{{form.status? $t('Shelves'):$t('TakenShelves')}}
					</view>
					<view class="" style="direction: ltr;">
						<switch :color="colortopic()" @change="change" :checked="form.status"
							style="transform:scale(0.7);direction: ltr;" />
					</view>
				</view>
			</view>
			<view class="save-button" @click="changesave">{{$t('Save')}}</view>
		</u-popup>
		<view class="footer-button">
			<view class="button-item" @click="addcategory('add')">
				<u-icon name="plus-circle" :color="colortopic()" size="22"></u-icon>
				<text>{{$t('AddCategory')}}</text>
			</view>
		</view>
		<u-toast ref="uToast" :message="notmessage"></u-toast>
	</view>
</template>

<script>
	import {
		Translation,
	} from '@/utils/getAddres.js';
	import {
		getcategory,
		postcategory,
		putcategory,
		deletecategory
	} from '@/api/shop.js';

	export default {
		data() {
			return {
				pickershow: false,
				fileList1: [],
				form: {
					ug_name: '',
					cn_name: '',
					status: true,
				},
				columns: [],
				notmessage: '请仔细填写!',
				popshow: false,
				edit_id: '',


			};
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			},
		},
		onLoad() {
			this.getCategory();
		},
		methods: {
			addcategory(item) {
				if (item!='add') {
					this.form.ug_name = item.ug_name
					this.form.cn_name = item.cn_name
					this.form.status = item.status
					this.edit_id = item.id;
				}else{
					this.form.ug_name = ''
					this.form.cn_name = ''
					this.form.status = true
				}
				this.popshow = true;
			},
			change(e) {
				this.form.status = e.detail.value;
			},
			open() {},
			close() {
				this.popshow = false
			},
			changeconfrem(e) {
				this.pickershow = false;
				if (this.$i18n.locale == 'ug') {
					this.form.type = e.value[0].ug_name;
					let Trantext = Translation('uy', this.form.type, 'zh');
				} else {
					this.form.type = e.value[0].cn_name;
				}
			},
			async changeugblur() {
				try {
					if (this.form.cn_name == "" && this.form.ug_name != "") {
						let Trantext = await Translation('uy', this.form.ug_name, 'zh');
						this.form.cn_name = Trantext;
					}
				} catch (error) {
					console.error('Error:', error);
				}

			},
			async changecnblur() {
				try {
					if (this.form.ug_name == "" && this.form.cn_name != "") {
						let Trantext = await Translation('zh', this.form.cn_name, 'uy');

						this.form.ug_name = Trantext;
					}
				} catch (error) {
					console.error('Error:', error);
				}

			},
			//获取分类
			getCategory() {
				let that = this;
				getcategory({}).then((res) => {
					if (res.code == 200) {
						let data = res.body.data;
						that.columns = data;
					}
				})
			},
			//添加分类
			changesave() {
				let that = this;
				this.popshow = false;
				if (!that.form.ug_name && !that.form.cn_name) {
					that.$refs.uToast.show({
						top: 0,
						message: this.$t('TableEmpty'),
						color: '#fff',
						bgColor: '#f0ba53'
					})
					return;
				}
				if (that.edit_id) {
					putcategory({
						id: that.edit_id,
						ug_name: that.form.ug_name,
						cn_name: that.form.cn_name,
						status: that.form.status
					}).then((res) => {
						if (res.code == 200) {
							that.getCategory();
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('EditSuccessful'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						} else {
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('EditFailed'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						}
					})
				} else {
					postcategory({
						ug_name: that.form.ug_name,
						cn_name: that.form.cn_name,
						status: that.form.status
					}).then((res) => {
						if (res.code == 200) {
							that.getCategory();
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('AdditionSuccessful'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						} else {
							that.$refs.uToast.show({
								top: 0,
								message: this.$t('AdditionFailed'),
								color: '#fff',
								bgColor: '#f0ba53'
							})
						}
					})
				}

			},
			//删除分类
			deletcategory(id) {
				let that = this;
				deletecategory({
					id: id
				}).then((res) => {
					if (res.code == 200) {
						that.getCategory();
						that.$refs.uToast.show({
							top: 0,
							message: this.$t('DeleteSuccessful'),
							color: '#fff',
							bgColor: '#f0ba53'
						})
					} else {
						that.$refs.uToast.show({
							top: 0,
							message: this.$t('DeleteFailed'),
							color: '#fff',
							bgColor: '#f0ba53'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.personal {
		padding: $uni-spacing-padding-sm;
		box-sizing: border-box;
		width: 100%;

		.category-box {
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			font-size: $uni-font-size-base;

			.cate-item {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				box-sizing: border-box;
				background-color: $uni-bg-color-box;
				border-radius: $uni-border-radius-base;
				box-shadow: $uni-bg-shadow;
				margin-bottom: 30rpx;
				padding: $uni-spacing-padding-sm;

				.left {
					display: flex;
					align-items: center;

					text {
						padding: 0rpx 20rpx;
					}
				}

				.right {
					display: flex;
					align-items: center;
					color: $uni-text-color-grey;
					font-size: $uni-font-size-sm1;

					.button {
						padding: 5rpx 10rpx;
						border-radius: $uni-border-radius-lg;
						margin: 0rpx 10rpx;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;

						text {
							padding: 10rpx 0rpx;
						}
					}

				}
			}
		}

		.input-from {
			width: 600rpx;
			height: 500rpx;
			padding: 10rpx 20rpx;
			box-sizing: border-box;

			.big-title {
				width: 100%;
				color: $uni-color-topic;
				font-size: $uni-font-size-lg;
				padding: 20rpx 0rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				text {
					padding: 0rpx 20rpx;
				}
			}

			.input-item {
				width: 100%;
				// display: flex;
				padding: $uni-spacing-padding-sm;
				box-sizing: border-box;

				input {
					font-size: $uni-font-size-base;

				}

				.title {
					color: $uni-color-title;
					font-size: $uni-font-size-base;
				}
			}
		}

		.save-button {
			width: 70%;
			margin: 20rpx auto;
			background-color: $uni-color-topic;
			color: #fff;
			font-size: $uni-font-size-lg;
			padding: $uni-spacing-padding-sm 0rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: $uni-border-radius-lg;
		}

		.footer-button {
			width: 100%;
			height: 100rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-around;
			position: fixed;
			bottom: 0;
			left: 0;

			.button-item {
				width: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			text {
				padding: 0rpx 20rpx;
			}
		}
	}

	.ug-from {
		direction: rtl;

		/deep/ .uni-input-input {
			direction: rtl !important;
			text-align: right;
		}

		/deep/ .uni-input-placeholder {
			text-align: right;
		}
	}
</style>